package edu.tgc.web.controller.system;

import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.abyss.common.exception.ServiceException;
import org.abyss.common.web.controller.easyui.EasyuiResultBuilder;
import org.abyss.common.web.controller.easyui.tree.CommonCrudEasyUiTreeController;
import org.abyss.common.web.controller.easyui.tree.EasyUiTreeEntityMapBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.tgc.entity.system.SysDept;
import edu.tgc.entity.system.SysPosition;
import edu.tgc.entity.system.SysRole;
import edu.tgc.service.system.SysDeptService;
import edu.tgc.service.system.SysPositionService;
import edu.tgc.web.form.system.SysDeptForm;

@Controller
@RequestMapping(value = "sys/dept")
public class SysDeptController extends CommonCrudEasyUiTreeController<SysDept, UUID, SysDeptForm> {

    @Autowired
    protected SysDeptService sysDeptService;

    @Autowired
    protected SysPositionService sysPositionService;

    @Override
    protected void buildIgnoreProperties(Collection<String> ignoreProperties) {
        ignoreProperties.add("childrens");
        ignoreProperties.add("fuctions");
        ignoreProperties.add("roles");
        ignoreProperties.add("positions");
        ignoreProperties.add("userPositions");
    }

    public Object getDeptTree(UUID id) {
        List<SysDept> list = this.sysDeptService.getListById(id);
        return list;
    }

    @ResponseBody
    @RequestMapping(value = "position/selected")
    public Object getPositions(String joinIds) {
        Set<SysPosition> result = new LinkedHashSet<>();
        if (joinIds != null) {
            for (String entityId : joinIds.split(",")) {
                SysDept entity = this.baseService.getById(UUID.fromString(entityId));
                if (entity != null) {
                    result.addAll(entity.getPositions());
                }
            }
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "role/selected")
    public Object getRoles(String joinIds) {
        Set<SysRole> result = new HashSet<>();
        if (joinIds != null) {
            for (String entityId : joinIds.split(",")) {
                SysDept entity = this.baseService.getById(UUID.fromString(entityId));
                if (entity != null) {
                    result.addAll(entity.getRoles());
                }
            }
        }
        return result;
    }

    @Override
    protected EasyUiTreeEntityMapBuilder<SysDept> getTreeGridEntityMapBuilder(SysDeptForm form) {
        final Set<SysDept> checkedList = new HashSet<>();
        String ids = form.getJoinIds();
        if (StringUtils.hasText(ids)) {
            for (String id : ids.split(",")) {
                SysPosition entity = this.sysPositionService.getById(UUID.fromString(id));
                if (entity != null) {
                    checkedList.addAll(entity.getDepts());
                }
            }
        }
        return new EasyUiTreeEntityMapBuilder<SysDept>() {
            @Override
            public void build(Map<String, Object> entityMap, SysDept entity, List<SysDept> children) {
                if (checkedList.size() != 0 && checkedList.contains(entity)) {
                    entityMap.put("selected", true);
                }
                entityMap.put("iconCls", "icon-dept");
                entityMap.put("id", entity.getId());
                entityMap.put("name", entity.getName());
                entityMap.put("description", entity.getDescription());
                entityMap.put("sort", entity.getSort());
                entityMap.put("enabled", entity.getEnabled());
                entityMap.put("createTime", entity.getCreateTime());
                entityMap.put("lastModifyTime", entity.getLastModifyTime());
            }
        };
    }

    @ResponseBody
    @RequestMapping(value = "role", method = RequestMethod.POST)
    public Object role(SysDeptForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            return EasyuiResultBuilder.buildAjaxResultError(bindingResult.getGlobalError().getDefaultMessage());
        } else {
            try {
                this.sysDeptService.setRoles(form.getId(), form.getRoleIds());
            } catch (ServiceException e) {
                return EasyuiResultBuilder.buildAjaxResultError(e.getMessage());
            }
        }
        return EasyuiResultBuilder.buildAjaxResult();
    }

    @RequestMapping(value = "subcompany")
    @ResponseBody
    public Object subcompany() {
        return this.sysDeptService.subcompany();
    }

/*    @RequestMapping(value = "startProjectDept")
    @ResponseBody
    public Object startProjectDept() {
        return this.sysDeptService.getStartProjectDept();
    }*/
}
